package com.alks.yn_zs.service.serviceImpl;

import com.alks.entity.data.page.PageParam;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.yn_zs.data.dto.PcStitchStockDto;
import com.alks.yn_zs.data.vo.StitchStockVO;
import com.alks.yn_zs.mapper.StitchUpperShowMapper;
import com.alks.yn_zs.service.StitchUpperShowService;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
@RequiredArgsConstructor
public class StitchUpperShowServiceImpl implements StitchUpperShowService {

    final private StitchUpperShowMapper stitchUpperShowMapper;


    /**
     * 鞋面日出入库状况
     *
     * @param
     * @return
     */
    @Override
    public ResponseInfo getDayStitchInOutQty() {
        //获取公司，默认
        String companyId = "BF";

        LocalDate yesterday = LocalDate.now().minusDays(1); // 例如：今天日期：2025-10-15，查询的是2025-10-15
        List<StitchStockVO> list = stitchUpperShowMapper.getDayStitchInOutQty(companyId,yesterday);

        //总库存
        BigDecimal stockSum = stitchUpperShowMapper.queryStitchStockSum(companyId);


        Map<String, Object> map = new HashMap<>();
        BigDecimal inQty = BigDecimal.ZERO;
        BigDecimal outQty = BigDecimal.ZERO;

        for (StitchStockVO stockQtyVO : list) {
            String slipType = stockQtyVO.getSlipType();
            BigDecimal qty = stockQtyVO.getQty();

            // 判空处理，防止数据库返回 NULL
            if (qty == null) {
                qty = BigDecimal.ZERO;
            }

            if ("I".equals(slipType)) {
                inQty = qty;
            } else if ("O".equals(slipType)) {
                outQty = qty;
            }
        }


        //Map<String, Object> map = new HashMap<>();
        //BigDecimal inQty = BigDecimal.ZERO;
        //BigDecimal outQty = BigDecimal.ZERO;
        //
        //for (StitchStockVO stockQtyVO : list) {
        //    String slipType = stockQtyVO.getSlipType();
        //    BigDecimal qty = stockQtyVO.getQty();
        //
        //    // 判空处理，防止数据库返回 NULL
        //    if (qty == null) {
        //        qty = BigDecimal.ZERO;
        //    }
        //
        //    if ("I".equals(slipType)) {
        //        inQty = inQty.add(qty);
        //    } else if ("O".equals(slipType)) {
        //        outQty = outQty.add(qty);
        //    }
        //}
        //
        //// 计算库存净变动：入库 - 出库
        //BigDecimal stockSum = inQty.subtract(outQty);

        map.put("inQty", inQty);
        map.put("outQty", outQty);
        map.put("stockSum", stockSum);

        return ResponseInfo.ok(map);
    }

    /**
     * 月出入库状况
     *
     * @return
     */
    @Override
    public ResponseInfo getMonthStitchInOutQty() {
        //获取公司，默认
        String companyId = "BF";

        // 计算“昨天”
        LocalDate yesterday = LocalDate.now().minusDays(1); // 例如：今天10月22日，取2025-10-21

        // 获取当前月的第一天（即“当月1号”）
        LocalDate startDate = yesterday.withDayOfMonth(1); // 例如：2025-10-01

        List<StitchStockVO> list = stitchUpperShowMapper.getMonthStitchInOutQty(companyId,startDate,yesterday);

        Map<String, Object> map = new HashMap<>();

        for (StitchStockVO stockQtyVO : list) {
            String slipType = stockQtyVO.getSlipType();
            if ("I".equals(slipType)) {
                map.put("inQty", stockQtyVO.getQty());
            } else if ("O".equals(slipType)) {
                map.put("outQty", stockQtyVO.getQty());
            }
        }

        return ResponseInfo.ok(map);
    }

    /**
     * 月出入库明细状况
     * @return
     */
    @Override
    public ResponseInfo getMonthDetailStitchInOutQty() {

        //获取公司，默认
        String companyId = "BF";

        // 计算“昨天”
        LocalDate yesterday = LocalDate.now().minusDays(1); // 例如：今天10月22日，取2025-10-21

        // 获取当前月的第一天（即“当月1号”）
        LocalDate startDate = yesterday.withDayOfMonth(1); // 例如：2025-10-01

        List<StitchStockVO> list = stitchUpperShowMapper.getMonthDetailStitchInOutQty(companyId,startDate,yesterday);

        return ResponseInfo.ok(list);
    }

    /**
     * 鞋面库存明细
     * @return
     */
    @Override
    public ResponseInfo getStitchUpperDetail(PageParam pageParam) {
        String companyId = "BF";

        /*获取数据*/
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        List<PcStitchStockDto> stockDtos = stitchUpperShowMapper.queryStitchStockHead(pageParam,companyId);
        Page<PcStitchStockDto> page=(Page<PcStitchStockDto>) stockDtos;

        /*封装结果*/
        Map map=new HashMap();
        map.put("dto",stockDtos);
        return ResponseInfo.ok(map);
    }
}
